<template>
  <el-table :data="filterTableData" style="width: 100%">

    <el-table-column label="标题" prop="title" />

    <el-table-column label="userid" prop="userid" />

    <el-table-column label="内容">
      <template #default="scope">
        <el-tooltip class="item" effect="dark" :content="scope.row.content" placement="top">
          <span>{{ truncateContent(scope.row.content) }}</span>
        </el-tooltip>
      </template>
    </el-table-column>
    
    <el-table-column label="标签">
      <template #default="scope">
        <el-radio-group v-model="activeTag" size="small" class="tag-group">
          <el-radio-button 
            v-for="(tag, index) in scope.row.tags" 
            :key="index" 
            :label="tag">
            {{ tag }}
          </el-radio-button>
        </el-radio-group>
      </template>
    </el-table-column>

    <el-table-column align="right">
      <template #header>
        <el-input v-model="search" size="small" placeholder="请输入标签查找"></el-input>
      </template>
      <template #default="scope">
        <el-button
          size="small"
          type="danger"
          @click="handleDelete(scope.$index, scope.row)"
        >
          删除
        </el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<script lang="ts" setup>
import { computed, onMounted, ref } from 'vue';
import { useArticlesStore } from '@/stores/articles';
import { useEntryStore } from '@/stores/entry';
import { ElTooltip } from 'element-plus';


const articles = useArticlesStore();
const entryStore = useEntryStore();


interface User {
  id: number;
  title: string;
  content: string;
  userid: number;
  username:string;
  tags: string[]; 
}

const open = ref<boolean>(false);
let search = ref('');
const activeTag = ref<string>(''); 


const tableData = ref<User[]>([]);


async function getArticles() {
  const response = await articles.getArticle();
  tableData.value = response.data.data;
  articles.tableData.push(response.data.data);
}

onMounted(()=>{
  getArticles();
})

const filterTableData = computed(() =>
  tableData.value.filter(
    (data) =>
      !search.value ||
      data.title.toLowerCase().includes(search.value.toLowerCase())
  )
);


async function handleDelete(index: number, row: User) {
  tableData.value.splice(index, 1);
  const Id = row.id;
  await articles.deleteArticle(entryStore.token, Id);
}



const truncateContent = (content: string) => {
  return content.length > 20 ? content.slice(0, 20) + '...' : content;
};
</script>

<style scoped>

</style>
